package org.carabiner.infinitest;

import java.io.IOException;
import java.util.List;

public class SimpleProcessRunner implements TestRunner
{
  private RemoteProcessManager manager;
  private TestRunnerEventSupport eventSupport;

  public SimpleProcessRunner(Class<? extends TestRunner> runnerClass)
  {
    eventSupport = new TestRunnerEventSupport(this);
    manager = new RemoteProcessManager(runnerClass);
  }

  public void addTestStatusListener(TestStatusListener listener)
  {
    eventSupport.addTestStatusListener(listener);
  }

  public void killTest()
  {
    manager.destroyProcess();
  }

  @SuppressWarnings("unchecked")
  public void runTest(String testClass)
  {
    ProcessCommunicator communicator = new ProcessCommunicator();
    try
    {
      manager.startProcess(communicator.getPort(), new String[] { testClass });
    }
    catch (IOException e)
    {
      throw new RuntimeException("Could not start process for test " + testClass);
    }
    eventSupport.fireTestEvents((List<TestEvent>) communicator.getResults());
  }

}
